package com.lkd.http.controller;

import com.lkd.entity.OrderEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.OrderService;
import com.lkd.vo.PayVO;
import com.lkd.wxpay.WxPayDTO;
import com.lkd.wxpay.WxPaySDKUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxPaySDKUtil wxPaySDKUtil;

    @PostMapping("/requestPay")
    public String requestPay(@RequestBody PayVO payVO){
        if (payVO == null
                || Strings.isNullOrEmpty(payVO.getInnerCode())
                || Strings.isNullOrEmpty(payVO.getSkuId())
                || Strings.isNullOrEmpty(payVO.getOpenId())) throw new LogicException("请求参数错误");

        OrderEntity order = orderService.createOrder(payVO);

        WxPayDTO wxPayDTO = new WxPayDTO();
        wxPayDTO.setOpenid(payVO.getOpenId());
        wxPayDTO.setBody(order.getSkuName());
        wxPayDTO.setOutTradeNo(order.getOrderNo());
        wxPayDTO.setTotalFee(order.getAmount().intValue());




        return wxPaySDKUtil.requestPay(wxPayDTO);
    }

}
